package com.hzf.booklibrary.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import com.hzf.booklibrary.utils.ScreenUtils;

/**
 * Created by Administrator on 2017/5/10.
 * ViewGroup事件拦截和事件处理
 * 各个action返回值代表拦截当前action，交给onTouchEvent处理
 * onTouchEvent 处理事件完成后，若想要再次接收接下来的动作，必须返回true，代表处理该事件
 */

public class PdfViewerGroup extends FrameLayout {
    private OnReadPDFSateChangeListener listener;
    private int mScreenWidth;
    private int mScreenHeight;
    private int downX;
    private int downY;
    private static final int CENTER=0;
    private int clickState=-1;

    public PdfViewerGroup(Context context) {
        super(context);
        init();
    }

    public PdfViewerGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PdfViewerGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mScreenWidth = ScreenUtils.getScreenWidth();
        mScreenHeight = ScreenUtils.getScreenHeight();
        this.setClickable(true);
    }

    public void setOnReadPDFSateChangeListener(OnReadPDFSateChangeListener listener){
        this.listener=listener;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        downX= (int) ev.getX();
        downY = (int) ev.getY();
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(clickState!=-1){
                    clickState=-1;
                }
                if (downX >= mScreenWidth *0.35f && downX <= mScreenWidth * 0.65f&& downY >= mScreenHeight *0.3f && downY <= mScreenHeight * 0.7f) {
                    return true;
                }
                break;
//            case MotionEvent.ACTION_MOVE:
//            case MotionEvent.ACTION_UP:
//            case MotionEvent.ACTION_CANCEL:
//                if(clickState==CENTER){
//                    if (downX >= mScreenWidth *0.33f && downX <= mScreenWidth * 0.67f&& downY >= mScreenHeight *2/5 && downY <= mScreenHeight * 3 /5) {
//                        return true;
//                    }
//                }
//            break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    /**
     * 必须在actiondown的时候返回true，否则就不会再走抬起事件了
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int upX= (int) event.getX();
        int upY = (int) event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX= (int) event.getX();
                downY = (int) event.getY();
                if (downX >= mScreenWidth *0.35f && downX <= mScreenWidth * 0.65f
                        && downY >= mScreenHeight *0.3f && downY <= mScreenHeight * 0.7f) {
                        clickState=CENTER;
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if(clickState==CENTER){
                    if((Math.abs(upX - downX)<5) &&(Math.abs(upY - downY) < 5)){
                        listener.onCenterClick();
                        return true;
                    }
                }
                break;
        }
        return false;
    }
    public interface OnReadPDFSateChangeListener{
        void onCenterClick();
    }
}
